package pure.ui 
{
	import pure.ui.core.*;
	import pure.ui.groups.IGroupItem;
	import pure.ui.skins.*;
	import flash.display.Bitmap;
	import flash.events.MouseEvent;
	import flash.geom.*;
	import pure.ui.groups.List;
	
	/**
	 * 列表项目
	 * @author Des
	 * <皮肤种类> ---- IClickSkin
	 * 
	 * <TODO>使用List生成实例，不可单独使用！
	 */
	
public class ListItem extends LabelComponent
{
	public function ListItem(x:Number, y:Number, size:Point = null, skin:ISkin = null, hasLabel:Boolean = false) 
	{
		super(x, y, size, skin, hasLabel);
	}
	

	
	
	/*model*/
	
	protected var _isOver:Boolean;	// 是否移上
	
	protected var _selected:Boolean;	// 是否选中
	
	protected var _selectable:Boolean;	// 是否可选中
	
	/*cache*/
	
	protected var ____clickObserver:Function;	// 点击回调方法
	
	
	
	
	/**
	 * 观察点击状态
	 * @param observer		回调方法
	 **/
	public function setObserverClick(observer:Function):void
	{
		____clickObserver = observer;
	}
	
	
	
	
	
	/* ----------------------------------------------------    get / set    ----------------------------------------------------*/
	
	
	
	
	
	// 列表项目皮肤
	override public function set skin(value:ISkin):void
	{
		if (!value is IClickSkin)
		{
			throw new Error('ListItem::skin - 皮肤类型错误！');
		}
		super.skin = value;
	}

	// 是否可选中
	public function set selectable(b:Boolean):void
	{
		if (this._selectable == b) 
		{
			return;
		}
		this._selectable = b;
		if (!this._selectable) 
		{
			this.selected = false;
		}
	}
	
	public function get selectable():Boolean
	{
		return _selectable;
	}
	
	// 是否被选中
	public function set selected(value:Boolean):void
	{
		if (!_selectable && this._selected == value)
		{
			return;
		}
		this._selected = value;
		update();
	}
	
	public function get selected():Boolean
	{
		return this._selected;
	}
	
	
	
	
	
	/* ----------------------------------------------------    private    ----------------------------------------------------*/
	
	
	
	
	
	override protected function _initSelf():void
	{
		super._initSelf();
		this.mouseChildren = false;
		this._selected = false;
		this._isOver = false;
		this._padding = 0;
		this.addEventListener(MouseEvent.ROLL_OVER, ____onMouseOver);
		this.addEventListener(MouseEvent.CLICK, ____onClick);
	}
	
	/**
	 * 子元素
	 **/
	override protected function _addChildren():void
	{
		super._addChildren();
		this._bitmap = new Bitmap();
		addChild(this._bitmap);
	}
	
	/**
	 * 选择
	 **/
	protected function ____onClick(e:MouseEvent):void
	{
		if (!this. _selected) 
		{
			var items:Array= this._group.items;
			var item:ListItem;
			var len:int = _group.numItem;
			for (var i:int = 0; i < len; i++)
			{
				item = items[i];
				if (item.selected)
				{
					item.selected = false;
					break;
				}
			}
			this._group.selectedId = this._id;
			this.selected = true;
		}
		if (____clickObserver != null)
		{
			____clickObserver();
		}
	}

	/**
	 * 移上
	 **/
	protected function ____onMouseOver(e:MouseEvent):void
	{
		this._isOver = true;
		this.addEventListener(MouseEvent.ROLL_OUT, ____onMouseLeave);	
		update();
	}
	
	/**
	 * 移出
	 **/
	protected function ____onMouseLeave(e:MouseEvent):void
	{
		this.removeEventListener(MouseEvent.ROLL_OUT, ____onMouseLeave);
		this._isOver = false;
		update();
	}
	
	/**
	 * 绘制
	 */
	override protected function _draw():void
	{
		// 选中
		if (this._selectable&&this._selected)
		{
			if (this._skin)
			{
				if (_getBitmap("down"))
				{
					_skip("down");
					_clear();
				}
				else 
				{
					if (this._bitmap.bitmapData)
					{
						this._bitmap.bitmapData = null;
					}
					_definition = null;
					_drawNoSkin(SkinColor.List_SELECT,1,4);
				}
			}
			else 
			{
				_drawNoSkin(SkinColor.List_SELECT,1,4);
			}
			return;
		}
		
		// 滑过
		if (this._isOver)
		{
			if (this._skin)
			{
				if (_getBitmap("over")) 
				{
					_skip("over");
					_clear();
				}
				else 
				{
					if (this._bitmap.bitmapData) 
					{
						this._bitmap.bitmapData = null;
					}
					_definition = null;
					_drawNoSkin(SkinColor.LIST_OVER,1,4);
				}
			}
			else 
			{
				_drawNoSkin(SkinColor.LIST_OVER,1,4);
			}
			return;
		}
		
		// 移出
		if (this._skin)
		{
			if (_getBitmap("out")) 
			{
				_skip("out");
				_clear();
			}
			else
			{
				if (this._bitmap.bitmapData) 
				{
					this._bitmap.bitmapData = null;
				}
				_definition = null;
				_drawNoSkin(SkinColor.LIST_OUT,1,4);
			}
		}
		else 
		{
			_drawNoSkin(SkinColor.LIST_OUT,1,4);
		}
		
		super._draw();
	}

}

}